<?php
class SeoController extends CController {
	public $defaultAction = 'listLinks';

	public function filters() {
		return array(
				'accessControl'
		);
	}
	
	
	public function accessRules() {
		return array(
			array(
				'allow',
				'expression' => ForumMembers::isAdmin()
			),
			array(
				'deny',
			),
		);
	}
	
	public function actionListLinks() {
		$links = Links::model()->findAll();
		
		$this->render('links-list', array(
				'links' => $links
			)
		);
	}
	
	public function actionEditLink() {
		$linkId = isset($_GET['id']) ? (int)$_GET['id'] : null;
		if($linkId !== null) {
			$link = Links::model()->findByPk($linkId);
			if($link !== null) {
				if(isPost()) {
					$link->attributes = $_POST['Links'];
					$link->sortOrder = (int)$link->sortOrder;
					$link->save();
				}

				$this->render('edit-link', array(
						'link' => $link
					)
				);
			}
		}
	}
	
	public function actionAddLink() {
		$link = new Links;
		if(isPost()) {
			$link->attributes = $_POST['Links'];
			$link->sortOrder = (int)$link->sortOrder;
			if($link->save()) {
				$message = '友情链接添加成功。';
				Yii::app()->user->setFlash('message', $message);
			}
		}
		
		$this->render('edit-link', array(
				'link' => $link
			)
		);
	}
	
	public function actionDeleteLink() {
		$linkId = isset($_GET['id']) ? (int)$_GET['id'] : null;
		
		if($linkId !== 0) {
			$link = Links::model()->findByPk($linkId);
			if($link !== null && $link->delete()) {
				$message = '友情链接删除成功。';
				Yii::app()->user->setFlash('message', $message);
				$this->redirect(array('/admin/seo/listlinks'));
			}
		}
	}
}